<?php

namespace app\admin\controller\referral\config;

use app\BaseController;
use app\model\referral\ReferralAgencyModel;
use app\model\referral\ReferralBindModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 机构
     * @return bool|string
     */
    public function agency(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require'
            ]);
            $ReferralAgencyModel = new ReferralAgencyModel();
            $ReferralBindModel = new ReferralBindModel();
            $data = $ReferralAgencyModel->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$value) {
                $bind = $ReferralBindModel->where([
                    ['unit_id','=',$this->userData['unit_id']],
                    ['agency_id','=',$value['id']]
                ])->order('id','desc')->find();
                if ($bind){
                    $value['reason'] = $bind['reason'];
                    $value['status'] = $bind['status'];
                    $value['time'] = $bind['time'];
                }else{
                    $value['reason'] = '';
                    $value['status'] = 0;
                    $value['time'] = 0;
                }
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 申请绑定
     * @return bool|string
     */
    public function bind(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'agency_id|机构' => 'require',
                'reason|申请理由' => 'require',
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['uid'] = $this->userData['id'];
            $param['up_time'] = time();
            $param['time'] = time();
            $ReferralBindModel = new ReferralBindModel();
            $ReferralBindModel->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 解除绑定
     * @return bool|string
     */
    public function delBind(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require'
            ]);
            $ReferralBindModel = new ReferralBindModel();
            $info = $ReferralBindModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save([
                'is_del' => 1,
                'del_time' => time()
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 已绑定记录
     * @return bool|string
     */
    public function bingLog(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require'
            ]);
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $where[] = ['status','=',2];
            $ReferralBindModel = new ReferralBindModel();
            $data = $ReferralBindModel->where($where)->with([
                'user',
                'bindAgency'
            ])->paginate($param['pageSize'])->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}